package MyGui;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

import BackGround.*;
import DistSys.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

public class EditBgLineDlg extends MyDlgFrame implements WindowListener, ActionListener{
  private static final String TITLE_ONE="Edit Single Line";
  private static final String TITLE_TWO="Edit Multiple Lines";
  private static final String STR_KEEP= "[Keep]";
  private static final String STR_CHOOSE= "  "; //no text in [choose] mode

  private JLabel      lblName, lblFontSize, lblThickness, lblStyle, lblColor, lblColorMode;
  private JTextField  tfName, tfFontSize, tfThickness;
  private JComboBox   cmbStyle;
  private JButton     btnColor, btnRefresh, btnSetAsDft, btnUpdateSelection;

  public EditBgLineDlg(MainFrame parent, String title ){
      super( parent, title );
      init();
  }

  public void init(){
      lblName = new JLabel("Name");
      lblName.setFont( GuiConst.fontDefault);
      lblFontSize = new JLabel("Font Size");
      lblFontSize.setFont( GuiConst.fontDefault);
      lblThickness = new JLabel("Thickness");
      lblThickness.setFont( GuiConst.fontDefault);
      lblStyle = new JLabel("Style");
      lblStyle.setFont( GuiConst.fontDefault);
      lblColor = new JLabel("Color");
      lblColor.setFont( GuiConst.fontDefault);
      lblColorMode = new JLabel();
      lblColorMode.setFont( GuiConst.fontDefault);

      tfName = new JTextField(12);
      tfName.setFont( GuiConst.fontDefault);
      tfFontSize = new JTextField(8);
      tfFontSize.setFont( GuiConst.fontDefault);
      //tfFontSize.setInputVerifier( new PosAndZeroIntVerifier() );
      tfThickness= new JTextField(8);
      tfThickness.setFont( GuiConst.fontDefault);
      //tfThickness.setInputVerifier( new PosAndZeroIntVerifier() );

      cmbStyle = new JComboBox( BackGround.BgLine.LINE_STYLE );
      cmbStyle.setFont( GuiConst.fontDefault);

      btnColor = new JButton();
      btnColor.setFont( GuiConst.fontDefault);
      btnRefresh = new JButton("Refresh Drawing");
      btnRefresh.setFont( GuiConst.fontDefault);
      btnSetAsDft = new JButton("Set as New Default");
      btnSetAsDft.setFont( GuiConst.fontDefault);

      GridBagLayout gridBag = new GridBagLayout();
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(2,2,2,2);
      gbc.fill = GridBagConstraints.BOTH;

      Container pane = this.getContentPane();
      pane.setLayout( gridBag );

      gbc.gridx=0;  gbc.gridy=0;  gbc.gridwidth=1;  pane.add( lblName, gbc);
      gbc.gridx=1;  gbc.gridy=0;  gbc.gridwidth=2;  pane.add( tfName, gbc);
      gbc.gridx=0;  gbc.gridy=1;  gbc.gridwidth=1;  pane.add( lblFontSize, gbc);
      gbc.gridx=1;  gbc.gridy=1;  gbc.gridwidth=2;  pane.add( tfFontSize, gbc);
      gbc.gridx=0;  gbc.gridy=2;  gbc.gridwidth=1;  pane.add( lblThickness, gbc);
      gbc.gridx=1;  gbc.gridy=2;  gbc.gridwidth=2;  pane.add( tfThickness, gbc);
      gbc.gridx=0;  gbc.gridy=3;  gbc.gridwidth=1;  pane.add( lblStyle, gbc);
      gbc.gridx=1;  gbc.gridy=3;  gbc.gridwidth=2;  pane.add( cmbStyle, gbc);
      gbc.gridx=0;  gbc.gridy=4;  gbc.gridwidth=1;  pane.add( lblColor, gbc);
      gbc.gridx=1;  gbc.gridy=4;  gbc.gridwidth=1;  pane.add( btnColor, gbc);
      gbc.gridx=2;  gbc.gridy=4;  gbc.gridwidth=1;  pane.add( lblColorMode, gbc);
      gbc.gridx=0;  gbc.gridy=5;  gbc.gridwidth=3;  pane.add( btnRefresh, gbc);
      gbc.gridx=0;  gbc.gridy=6;  gbc.gridwidth=3;  pane.add( btnSetAsDft, gbc);
      //gbc.gridx=0;  gbc.gridy=7;  gbc.gridwidth=3;  pane.add( btnUpdateSelection, gbc);

      this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
      this.setLocation(200,200);
      this.setSize(200, 250);
      //this.setResizable(false);

      this.addWindowListener( this );   //windows listener
      tfName.addFocusListener( focusListener );
      tfFontSize.addFocusListener( focusListener );
      tfThickness.addFocusListener( focusListener );
      cmbStyle.addFocusListener( focusListener );
      //btnColor.addFocusListener( focusListener );
      btnColor.addActionListener( this );
      btnRefresh.addActionListener( this );
      btnSetAsDft.addActionListener( this );

      try{ updateInfo(); }
      catch(Exception e)  { JOptionPane.showMessageDialog(this, e); }
  }

  //public void setCurrentElem( BgLine line){ this.bgLine = line; }

  /**
   * If only one background line is selected, use this one to update text.
   * Other wise, update based on default bakcgorund line.
   */
  public void updateInfo() {
      BgLine bgLine;
      if( Sys.selectedList.size() == 0){
          tfName.setText( "" );
          tfFontSize.setText( "" );
          tfThickness.setText( "" );
          cmbStyle.setSelectedIndex( BgLine.KEEP );
          btnColor.setBackground( Color.gray);
          lblColorMode.setText( STR_CHOOSE );
          this.disableAllGuiCmp();
      } else if( Sys.selectedList.size() == 1 ){
          bgLine = (BgLine) Sys.selectedList.get( 0 );
          tfName.setText( bgLine.getName() );
          tfFontSize.setText( "" + bgLine.getFontSize() );
          tfThickness.setText( "" + bgLine.getThickness() );
          cmbStyle.setSelectedIndex( bgLine.getStyle() );
          btnColor.setBackground( bgLine.getColor() );
          lblColorMode.setText( STR_CHOOSE );
          this.enableAllGuiCmp();
      } else {
          tfName.setText( STR_KEEP );
          tfFontSize.setText( STR_KEEP );
          tfThickness.setText( STR_KEEP );
          cmbStyle.setSelectedIndex( BgLine.KEEP );
          //btnColor.setBackground( Color.gray );
          //display [keep] only at the first activation
          if( isAllColorSame() ) {
              lblColorMode.setText( STR_CHOOSE);
              btnColor.setBackground( ((BgLine)(Sys.selectedList.get(0))).getColor());
          }
          else {
              lblColorMode.setText( STR_KEEP );
              btnColor.setBackground( Color.gray);
          }
          this.enableAllGuiCmp();
      }
  }

  /**
   * find whether all colors of selected BgLine are same
   */
  private boolean isAllColorSame(){
      BgLine bgLine;
      int iClr = ((BgLine) Sys.selectedList.get(0)).getColor().getRGB();
      int sz = Sys.selectedList.size();
      for(int i=1; i<sz; i++){
          bgLine = (BgLine) Sys.selectedList.get(i);
          if( iClr != bgLine.getColor().getRGB() ) { return false; }
      }
      return true;
  }

  private void disableAllGuiCmp(){
      tfName.setEnabled( false );
      tfFontSize.setEnabled( false );
      tfThickness.setEnabled( false );
      cmbStyle.setEnabled( false );
      btnColor.setEnabled( false );
      btnRefresh.setEnabled( false );
      btnSetAsDft.setEnabled( false );
  }

  private void enableAllGuiCmp(){
      tfName.setEnabled( true );
      tfFontSize.setEnabled( true );
      tfThickness.setEnabled( true );
      cmbStyle.setEnabled( true );
      btnColor.setEnabled( true );
      btnRefresh.setEnabled( true );
      btnSetAsDft.setEnabled( true );
  }

  /**
   * implement action listener for
   * btnColor, btnRefresh, and btnSetAsDefault
   */
  public void actionPerformed( ActionEvent evt ){
      Object obj = evt.getSource();
      if( obj == btnColor ){
          Color clr = JColorChooser.showDialog( btnColor,
                              "iPAD Color Chooser", btnColor.getBackground());
          btnColor.setBackground( clr );
          int sz = Sys.selectedList.size();
          for(int i=0; i<sz; i++){
              ((BgLine)(Sys.selectedList.get(i))).setColor( clr);
          }

          //since selected lines have the same color
          lblColorMode.setText( STR_CHOOSE);
      }
      else if( obj == btnRefresh ){
          saveInfoToAllSelected();
          mainFrame.mainPanel.needRedraw = true;
          mainFrame.mainPanel.repaint();
      }
      else if( obj == this.btnSetAsDft ){
          saveToBgLine( BgLine.dftBgLine );
      }
  }

  /**
   * set as default and skip items with [keep].
   * No need to save color when
   */
   private void saveToBgLine( BgLine bgline){
      try{
          if( tfName.getText().equalsIgnoreCase(STR_KEEP)==false ) {
              bgline.setName( tfName.getText() ); }
          if( tfFontSize.getText().equalsIgnoreCase(STR_KEEP) == false ){
              bgline.setFontSize( Double.parseDouble( tfFontSize.getText())); }
          if( tfThickness.getText().equalsIgnoreCase( STR_KEEP)==false){
              bgline.setThickness( Integer.parseInt( tfThickness.getText())); }
          if( cmbStyle.getSelectedIndex() != BgLine.KEEP ) {
              bgline.setStyle( cmbStyle.getSelectedIndex() ); }
          if( lblColorMode.getText().equalsIgnoreCase( STR_KEEP )==false ){
              bgline.setColor( btnColor.getBackground() ); }
      }catch ( Exception e ){
          JOptionPane.showMessageDialog( this, e.toString() );
      }
  }//end of setAsDefault


  /**
   * save info displayed on the dlg to all background lines in Sys.selectedList
   */
  private void saveInfoToAllSelected(){
      int sz = Sys.selectedList.size();
      BgLine line;
      for(int i=0; i<sz; i++){
          line = (BgLine) Sys.selectedList.get(i);
          this.saveToBgLine( line );
      }
  }//end of saveInfoToAllSelected()



  /**
   * Implement WindowListener interfaces
   */
    public void windowOpened( WindowEvent evt ){ }
    public void windowClosed( WindowEvent evt ){ }
    public void windowIconified( WindowEvent evt ){ }
    public void windowDeiconified( WindowEvent evt ){ }
    public void windowActivated( WindowEvent evt ){
        try { updateInfo(); }
        catch(Exception e)  { JOptionPane.showMessageDialog(this, e); }
    }
    public void windowDeactivated( WindowEvent evt ){ }
    public void windowClosing( WindowEvent evt ){
        /*try { s.saveInfoToAllSelected(); }
        catch(Exception e)  { JOptionPane.showMessageDialog(this, e); }
        mainFrame.mainPanel.needRedraw = true;
        mainFrame.mainPanel.repaint();*/
    }

    /*============================================
  FocusListener
      1.focusGained()
      2.focusLost()
  ============================================*/
  private FocusListener focusListener = new FocusListener(){
    public void focusGained(FocusEvent evt){ }
    public void focusLost(FocusEvent evt){
        saveInfoToAllSelected();
    }
  };
}